import axios from "axios";

const API_BASE_URL = "http://localhost:3000";
const api = axios.create({
    baseURL: API_BASE_URL,
    timeout: 10000,
    headers: { "Content-Type": "application/json" },
});

// DOA API
// DOA: Data Access Object 数据访问对象
// 实际的操作是通过redux进行操作的

export const productsAPI = {
    // 获取全部商品
    getAll: () => api.get("/products"),
    // 根据 id 获取商品
    getById: (id) => api.get(`/products/${id}`),
    // 根据分类获取商品
    getByCategory: (categoryId) => api.get(`/products?category=${categoryId}`),
    // 获取秒杀商品
    getSeckill: () => api.get("/products?isSeckill=true"),
    // 获取团购商品
    getGroupBuy: () => api.get("/products?isGroupBuy=true"),
}

export const categoriesAPI = {
    // 获取全部分类
    getAll: () => api.get("/categories"),
    // 根据 id 获取分类
    getById: (id) => api.get(`/categories/${id}`),
};

export const cartAPI = {
    // 获取购物车列表
    getAll: () => api.get("/cart"),
    // 删除购物车商品
    delete: (id) => api.delete(`/cart/${id}`),
    // 添加到购物车
    add: (item) => api.post("/cart", item),
    // 更新购物车商品
    update: (id, item) => api.patch(`/cart/${id}`, item),
    // 清空购物车
    clear: () => api.update("/cart", []),
}